package net.maku.module.organization.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.module.organization.dao.RoleStaffDao;
import net.maku.module.organization.entity.RoleStaffEntity;
import net.maku.module.organization.query.RoleStaffQuery;
import net.maku.module.organization.service.RoleStaffService;
import net.maku.module.organization.vo.RoleStaffVO;
import net.maku.module.staff.entity.StaffEntity;
import net.maku.newbase.NewBaseServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 业务_角色
 *
 * @author
 */
@Service
@AllArgsConstructor
public class RoleStaffServiceImpl extends NewBaseServiceImpl<RoleStaffDao, RoleStaffEntity> implements RoleStaffService {

    @Override
    public IPage pageByRoleId(IPage iPage, RoleStaffQuery roleStaffQuery) {
        String sql = "SELECT\n" +
                "	bs.id ,\n" +
                "	bs.NAME as name,\n" +
                "	bs.CODE as code,\n" +
                "	bo.abbreviation AS organName,\n" +
                "	bp.NAME AS positionName,\n" +
                "	bs.phone \n" +
                "FROM\n" +
                "	b_role_staff brs,\n" +
                "	b_staff bs,\n" +
                "	b_organization bo,\n" +
                "	b_position bp \n" +
                "WHERE\n" +
                "	brs.role_id = #{roleId} \n" +
                "	AND bs.id = brs.staff_id \n" +
                "	AND bo.id = bs.organization_id \n" +
                "	AND bp.id = bs.position_id";
        sql = sqlVO(sql, roleStaffQuery);
        return freedomMapper.pageJson(iPage, sql);
    }

    @Override
    public IPage<StaffEntity> pageStaff(IPage iPage, RoleStaffQuery roleStaffQuery) {
        String sql = "SELECT\n" +
                "	* \n" +
                "FROM\n" +
                "	b_staff bs \n" +
                "WHERE\n" +
                "	bs.id NOT IN (\n" +
                "	SELECT\n" +
                "		brs.staff_id \n" +
                "	FROM\n" +
                "		b_role_staff brs \n" +
                "WHERE\n" +
                "	brs.role_id = #{roleId})";
        sql = sqlVO(sql, roleStaffQuery);
        return fn(freedomMapper.pageJson(iPage, sql),StaffEntity.class);
    }
}